package cn.blog.controller.front;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.blog.controller.BaseServlet;
import cn.blog.entity.addBean.BlogArtInfo;
import cn.blog.entity.origin.Article;
import cn.blog.entity.origin.User;
import cn.blog.service.front.CompileHeadService;
import cn.blog.service.front.MainService;
@WebServlet("/front/main")
public class MainServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uname = (String) req.getSession().getAttribute("uname");
		
		//获取用户编号
		String userIdStr = req.getParameter("userId");
		int userId = Integer.parseInt(userIdStr);
		
		//根据用户id获取所有用户信息
		MainService ms = new MainService();
		User user = ms.getUser(userId);
		req.setAttribute("user", user);
		

		//获取用户园龄
		Date time1 = new Date();//系统时间
		Date time2 = user.getUserRegTime();//注册时间
        /* 先转成毫秒并求差 */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String d1 = sdf.format(time1);
        String d2 = sdf.format(time2);
        long days = 0;
		try {
			days = sdf.parse(d1).getTime()  - sdf.parse(d2).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long d = days / (1000 * 60 * 60 * 24);
        String day =  (d>30)?d/30+"月":d+"天";
        req.setAttribute("days",day);
        
        //根据用户id查找文章
        List<BlogArtInfo> artinfo = ms.getArticle(userId);
        req.setAttribute("art", artinfo);
     	
		
		
		forward("front/main");
	}

	
}
